#include "echo.pb.h"

#include "../../server/logging/Logging.h"
#include "../../server/net/EventLoop.h"
#include "../protorpc/RpcServer.h"

#include <unistd.h>

using namespace yhaida;

class EchoServiceImpl : public echo::EchoService
{
public:
    void Echo(::google::protobuf::RpcController *,
              const ::echo::EchoRequest *requst,
              ::echo::EchoResponse *response,
              ::google::protobuf::Closure *done) override
    {
        response->set_payload(requst->payload());
        done->Run();
    }
};

int main(int argc, char *argv[])
{
    int nThreads = argc > 1 ? atoi(argv[1]) : 1;
    LOG_INFO << "pid = " << getpid() << " threads = " << nThreads;
    EventLoop loop;
    int port = argc > 2 ? atoi(argv[2]) : 8888;
    InetAddress listenAddr(static_cast<uint16_t>(port));
    EchoServiceImpl impl;
    rpc::RpcServer server(&loop, listenAddr);
    server.setThreadNum(nThreads);
    server.registerService(&impl);
    server.start();
    loop.loop();
}
